package main

import (
	"vi/controller/fbc"

	"time"

	ratelimit "github.com/JGLTechnologies/gin-rate-limit"
	"github.com/gin-gonic/gin"
)

// 限流器
func keyFunc(c *gin.Context) string {
	return c.ClientIP()
}
func errorHandler(c *gin.Context, info ratelimit.Info) {
	c.String(429, "Too many requests. Try again in "+time.Until(info.ResetTime).String())
}
func route(r *gin.Engine) {
	// This makes it so each ip can only make 5 requests per second
	store := ratelimit.InMemoryStore(&ratelimit.InMemoryOptions{
		Rate:  time.Second,
		Limit: 1,
	})
	mw := ratelimit.RateLimiter(store, &ratelimit.Options{
		ErrorHandler: errorHandler,
		KeyFunc:      keyFunc,
	})
	// home
	r.POST("/test", func(ctx *gin.Context) {
		time.Sleep(6 * time.Second) // 模拟耗时操作
		ctx.String(200, "hello world")
	})

	// freeeipa
	r.POST("/run", mw, fbc.RunCode)
}
